package com.example.demo.service.impl;

import com.example.demo.common.Result;
import com.example.demo.dto.RouteAddDTO;
import com.example.demo.dto.RouteGetDTO;
import com.example.demo.entity.Route;
import com.example.demo.mapper.RouteMapper;
import com.example.demo.service.RouteService;
import com.example.demo.vo.ResponseData;
import com.example.demo.vo.RouteInfoVO;
import com.example.demo.vo.StopsInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class RouteServiceImpl implements RouteService {
    @Autowired
    private RouteMapper routeMapper;

    @Override
    public Result updateRoute(RouteAddDTO routeDTO) {
        Route route = new Route();
        route.setId(routeDTO.getId());
        route.setDeparture(routeDTO.getDeparture());
        route.setDestination(routeDTO.getDestination());
        StringBuilder sb = new StringBuilder();
        if(routeDTO.getStops().length > 0){
            sb = new StringBuilder(routeDTO.getStops()[0]);
        }
        for(int i=1; i<routeDTO.getStops().length; i++){
            sb.append(",").append(routeDTO.getStops()[i]);
        }
        route.setStops(sb.toString());
        int cnt = routeMapper.updateRoute(route);
        if(cnt > 0){
            return Result.success();
        }else{
            return Result.fail();
        }
    }

    @Override
    public Result getRoutes(RouteGetDTO routeGetDTO) {
        Route route = new Route();
        route.setDeparture(routeGetDTO.getDeparture());
        route.setDestination(routeGetDTO.getDestination());
        List<Route> routes = routeMapper.selectRoutes(route);
        ResponseData<Route> responseData = new ResponseData<>();
        responseData.setList(routes);
        responseData.setTotal(routes.size());
        return Result.success(responseData);
    }

    @Override
    public Result getStopsByCheciId(Integer checiId) {
        Map map = routeMapper.selectStopsByCheciId(checiId);
        StopsInfo stopsInfo = new StopsInfo();
        String stops = (String) map.get("stops");
        String startTime = (String) map.get("startTime");
        String endTime = (String) map.get("endTime");
        stopsInfo.setStops(stops.split(","));
        stopsInfo.setStartTime(startTime);
        stopsInfo.setEndTime(endTime);
        return Result.success(stopsInfo);
    }

    @Override
    public Result getAllRoutes() {
        Route route = new Route();
        List<Route> routes = routeMapper.selectRoutes(route);
        List<RouteInfoVO> routeInfoVOS = new ArrayList<>();
        for(Route route1 : routes){
            RouteInfoVO routeInfoVO = new RouteInfoVO();
            routeInfoVO.setId(route1.getId());
            routeInfoVO.setDescribe(route1.getDeparture()+" -> "+route1.getDestination());
            routeInfoVOS.add(routeInfoVO);
        }
        return Result.success(routeInfoVOS);
    }

    @Override
    public Result addRoute(RouteAddDTO routeAddDTO) {
        // 插入路线
        Route route = new Route();
        route.setDeparture(routeAddDTO.getDeparture());
        route.setDestination(routeAddDTO.getDestination());
        StringBuilder sb = new StringBuilder();
        if(routeAddDTO.getStops().length > 0){
            sb = new StringBuilder(routeAddDTO.getStops()[0]);
        }
        for(int i=1; i<routeAddDTO.getStops().length; i++){
            sb.append(",").append(routeAddDTO.getStops()[i]);
        }
        //把终点也算作是中途点，方便处理票价
        sb.append(",").append(routeAddDTO.getDestination());
        route.setStops(sb.toString());
        int cnt1 = routeMapper.insertRoute(route);

        if(cnt1 > 0){
            return Result.success();
        }else{
            return Result.fail();
        }
    }

}
